//
// Created by tcj on 2022/2/21.
//

#include "sylar/sylar.h"
#include "vector"
#include "unistd.h"
#include "vector"
sylar::Logger::ptr g_logger = SYLAR_LOG_NAME("root");
//sylar::RWMutex s_mutex;
sylar::Mutex s_mutex;
long count =0;
void fun1(){
    SYLAR_LOG_INFO(g_logger) << "name: "<<sylar::Thread::GetName()
                             << " this.name: "<<sylar::Thread::GetThis()->GetName()
                             << " id: "<<sylar::GetThreadId()
                             << " this.id: "<<sylar::Thread::GetThis()->getid();
    for(long i=0;i<1000000;++i){
//        sylar::RWMutex::WriteLock locker(s_mutex);
        sylar::Mutex::Lock locker(s_mutex);

        ++count;
//        SYLAR_LOG_INFO(g_logger) <<"thread1 test end";
    }
}

void fun2(){
    SYLAR_LOG_INFO(g_logger)<<"funs2 running";
//    while(true){
//        sleep(5);
        SYLAR_LOG_INFO(g_logger) <<"xxxxxxxxxxxxxxx";
//        break;
//    }


}

void fun3(){
    SYLAR_LOG_INFO(g_logger)<<"funs3 running";
    while(true){
        sleep(5);
        SYLAR_LOG_INFO(g_logger) <<"===============";
        break;
    }

}
std::vector<int> nums;

int main(int argc,char** argv){

    SYLAR_LOG_INFO(g_logger) <<"thread test begin";
//    YAML::Node root = YAML::LoadFile("/home/tcj/c++workspace/Server_framework/bin/conf/log2.yml");
//    sylar::Config::LoadFormYaml(root);
//    std::cout << sylar::loggerMar::GetInstance()->toYamlString() <<std::endl;

    std::vector<sylar::Thread::ptr> thrs;
    for(int i=0;i<2;i++){
        sylar::Thread::ptr thr(new sylar::Thread(&fun2,"name_"+std::to_string(i*2)));
        sylar::Thread::ptr thr2(new sylar::Thread(&fun3,"name_"+std::to_string(i*2+1)));
        thrs.push_back(thr);
        thrs.push_back(thr2);

    }

    SYLAR_LOG_INFO(g_logger) <<"thread test begin333";
    for(size_t i=0;i<thrs.size();i++){
        SYLAR_LOG_INFO(g_logger)<<"thread_name="<<thrs[i]->getName()
                    <<" running"<<std::endl;
        thrs[i]->join();
    }
    SYLAR_LOG_INFO(g_logger) <<"thread test end";
    SYLAR_LOG_INFO(g_logger) <<"count= "<<count;


    return 0;

}